package com.dfbz.service.impl;

import com.dfbz.dao.LoginDao;
import com.dfbz.entity.User;
import com.dfbz.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserServiceImpl extends IServiceImpl<User> implements UserService {

    @Autowired
    LoginDao loginDao;


    public Map<Object, Object> findUserInfo(Object userId) {
        Map<Object, Object> map = loginDao.findUserInfo(userId);

        Object login = map.get("login_time");
        Object register = map.get("register_time");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newLogin_time = sdf.format(login);
        String newRegister_time = sdf.format(register);

        map.replace("login_time", newLogin_time);
        map.replace("register_time",newRegister_time);

        return map;
    }

    @Override
    public PageInfo<User> pageConditionShow(Map<String, String> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<User> dbListUser=loginDao.selectPageCondition(params);

        PageInfo<User> pageInfo = new PageInfo<>(dbListUser);

        return pageInfo;
    }

    @Override
    public void deptIdUpdata(Integer id) {
        int dbUserDeptId = loginDao.deptNameUpdataId(id);
        loginDao.updateUserId(dbUserDeptId,id);
    }

    @Override
    public PageInfo<User> findFollowPageInfo(Integer pageNum, Integer pageSize, Integer id) {
        //首先这里是实现类
        PageHelper.startPage(pageNum,pageSize);
        List<User> followUserRealName = loginDao.findFollowRealName(id);

        PageInfo<User> userPageInfo = new PageInfo<>(followUserRealName);

        return userPageInfo;
    }

    @Override
    public List<User> followArticleMyFriend(String articleId, HttpServletRequest request) {

        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        Integer userId = loginUser.getId();

        List<User> userList=loginDao.followArticleMyFriend(articleId,userId);


        return userList;
    }

    @Override
    public List<Map<String, Object>> findAllDept() {
       List<Map<String, Object>> dbAllDeptList = loginDao.findAllDept();
        for (int i = 0; i < dbAllDeptList.size(); i++) {
            Map<String, Object> stringStringMap = dbAllDeptList.get(i);
            Object dept_id =stringStringMap.get("dept_id");

            List<Map<String,String>> dbDeptRealName=loginDao.findDeptRealName(dept_id.toString());

            stringStringMap.put("userList", dbDeptRealName);

        }
        return dbAllDeptList;
    }

}
